using System.Linq;
using UnityEngine;

namespace Services
{
    public class GetEmptyItemService
    {
        public static GetEmptyItemService Instance { private set; get; }=new GetEmptyItemService();
        private Contexts _contexts;
        public void Init(Contexts contexts)
        {
            _contexts = contexts;
        }

        public int GetNextEmptyRow(customData pos)
        {
            int row = pos.y;
            for (int i = pos.y - 1; i >= 0; i--)
            {
                var entity = _contexts.game
                    .GetEntitiesWithComponentsItemIndex(new customData(pos.x, i))
                    .ToArray();
                if (entity.Length == 0)
                {
                    row = i;
                }
                else
                {
                    if (!entity[0].isComponentsMovable)
                    {
                        continue;
                    }

                    break;
                }
            }
            return row;
        }
        
        
    }
}